package com.sljy.hzk.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.sljy.hzk.animation.AnimationCallType;
import com.sljy.hzk.util.EventListenerManager;

public class FocusEffct extends View {
    private int left;
    private int top;
    private int right;
    private int bottom;
    private int colorId;
    private Paint paint;
    private Rect rect;
    private RectF rectF;
    private boolean mIsRunning = false;
    private boolean mIsStartEffect = false;
    private float baseSpeed = 0.9f;
    private int speed = 100;
    private int focusWidth = 0;
    private int maxWidth = 500;
    private String tag = "FocusEffct";
    private boolean isForword = true;//前进
    private long time = 0;
    private float r = 0;
    private int curEvent;
    private OnTouchListener listener;

    public FocusEffct(Context context) {
        super(context);
    }

    public FocusEffct(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        rect = new Rect(0,0,right,bottom);
        rectF = new RectF(0,0,right,bottom);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);//设置空心
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setPathEffect(new CornerPathEffect(60));
        // mCreateRect.run();
    }

    public FocusEffct(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.i(tag,"onDraw");
        if(mIsRunning){
            if(isForword)//前进
                r= focusWidth+ (System.currentTimeMillis()-time)*baseSpeed;
            else
                r= focusWidth+ (time - System.currentTimeMillis())*baseSpeed;//后退
            Log.i(tag, (System.currentTimeMillis()-time)+"___"+r);
            if(r > bottom){
                if( curEvent == MotionEvent.ACTION_DOWN){
                    r = bottom;
                }else if(curEvent ==  MotionEvent.ACTION_UP){
                    mIsRunning = false;
                    paint.setColor(Color.WHITE);
                    r = 0;
                    //回调
                    listener.onEnd();
                }
            }
        }
        paint.setStrokeWidth(r);
        //canvas.drawCircle(right/2, bottom/2, bottom/1.5f,paint );
        canvas.drawRect(rect,paint);
        //canvas.drawRoundRect(rectF,50,50,paint);
        //postInvalidate();
        if(mIsRunning)
            postInvalidateDelayed(speed);
    }

    public void SetPaintColor(int colorId){
        paint.setColor(colorId);
    }

    public void touchEvent(int event) {
        curEvent = event;
        if(event == MotionEvent.ACTION_DOWN){
            speed = 30;
            isForword = true;
            start();
        }else if(event ==  MotionEvent.ACTION_OUTSIDE){
            isForword = false;
        }else if(event ==  MotionEvent.ACTION_UP){
            speed = 0;
            isForword = true;
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.i(tag,"onSizeChanged");
        right = w;
        bottom = h;
        maxWidth = w;
        rect.set(0,0,right,bottom);
        rectF.set(0,0,right,bottom);
    }

    public void setRectSize(int l, int t, int r, int b, int c){
        left = l;
        top = t;
        right = r;
        bottom = b;
        colorId = c;
    }

    private Runnable mCreateRect = new Runnable() {
        @Override
        public void run() {
            if (mIsRunning) {

            }
        }
    };

    public void start() {
        if (!mIsRunning) {
            time = System.currentTimeMillis();
            mIsRunning = true;
            postInvalidate();
        }
    }

    /**
     * 停止
     */
    public void stop() {
        mIsStartEffect = false;
        paint.setStrokeWidth(focusWidth);
    }

    public void OnEffctEnd(OnTouchListener l){
        listener = l;
    }

    public interface OnTouchListener{
        boolean onEnd();
    }

}
